<?php

namespace app\admin\controller;

use app\common\controller\Backend;
use think\Db;
use think\Request;
/**
 * 发帖，置顶帖支付金额记录
 *
 * @icon fa fa-circle-o
 */
class Usercircleprice extends Backend
{

    /**
     * Usercircleprice模型对象
     * @var \app\admin\model\Usercircleprice
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\Usercircleprice;
        $this->view->assign("ispayList", $this->model->getIspayList());
        $this->view->assign("duanList", $this->model->getDuanList());
    }



    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */


    /**
     * 查看
     */
    public function index()
    {
        //当前是否为关联查询
        $this->relationSearch = true;
        $circle_id=input('ids/d',0);
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            $where_c='1=1';

            $filter = $this->request->get('filter');
            $op = $this->request->get("op", '', 'trim');
            if($filter){
                $filter = json_decode($filter,true);

                $op = json_decode($op,true);
                if(isset($filter['istop'])){
                    
                    // "0":__('否'),"1":__('首页置顶'),"2":__('分类置顶'),"3":__('首页/分类置顶')
                    
       
                    if($filter['istop']=='0')
                    {
                        $where_c.=" and (index_top=0 and cate_top=0)";
                    }
                    if($filter['istop']=='1')
                    {
                        $where_c.=" and (index_top>0)";
                    }
                    if($filter['istop']=='2')
                    {
                        $where_c.=" and (cate_top>0)";
                    }
                    if($filter['istop']=='3')
                    {
                        $where_c.=" and (index_top>0 or cate_top>0)";
                    }
                    unset($filter['istop']);
                    unset($op['istop']);
                }




                $filter = json_encode($filter);
                $op = json_encode($op);
                Request::instance()->get(['filter'=>$filter]);
                Request::instance()->get(['op'=>$op]);
            }

            
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            
            
            if($circle_id)
            {
                $where_c.=" and  FIND_IN_SET($circle_id,circle_id)";
            }
            // var_dump($where_c);die;
            $total_pay = $this->model
                    ->with(['user'])
                    ->where($where)
                    ->where($where_c)
                    ->where(['ispay'=>'1'])
                    ->sum('price');
            $total_payrefund = $this->model
                    ->with(['user'])
                    ->where($where)
                    ->where($where_c)
                    ->where(['ispay'=>'1'])
                    ->where(['istui'=>'1'])
                    ->sum('price');
            $index_pay = $this->model
            ->with(['user'])
            ->where($where)
            ->where($where_c)
            ->where("index_top > 0")
            ->where(['ispay'=>'1'])
            ->sum('index_top_price');
            $cate_pay = $this->model
            ->with(['user'])
            ->where($where)
            ->where($where_c)
            ->where(['ispay'=>'1'])
            ->where("cate_top > 0")
            ->sum('cate_top_price');
            $top_pay = $this->model
            ->with(['user'])
            ->where($where)
            ->where($where_c)
            ->where("index_top > 0 or cate_top >0")
            ->where(['ispay'=>'1'])
            ->sum('cate_top_price+index_top_price');
            $list = $this->model
                    ->with(['user'])
                    ->where($where)
                    ->where($where_c)
                    ->where(['ispay'=>'1'])
                    ->order($sort, $order)
                    ->paginate($limit);

            foreach ($list as $row) {
                
                $row->getRelation('user')->visible(['username','tel']);
                //查询帖子名称
                $circle=db('user_circle')->where(['id'=>['in',$row->circle_id]])->select();
                $title='';
                foreach($circle as $value)
                {
                    $title=$value['title'].',';
                }
                $title=rtrim($title, ',');
                $row->data('circle_title',$title);
                $row->data('is_index_top','否');
                $row->data('is_cate_top','否');
                if($row->index_top>0)
                {
                    $row->data('is_index_top','置顶');
                    $row->data('is_cate_top','置顶');
                }
                
            }

            $result = array("total" => $list->total(), "rows" => $list->items(),'total_pay'=>round($total_pay,2),'total_refund'=>round($total_payrefund,2),'index_pay'=>round($index_pay,2),'cate_pay'=>round($cate_pay,2),'top_pay'=>round($top_pay,2));

            return json($result);
        }
        $this->assignconfig("ids", $circle_id);
        return $this->view->fetch();
    }

}
